package com.tensai.study.基础.底层.Volatile;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        myThread.start();
        for (;;){
            if (myThread.isFlag()) {
                System.out.println("+++++++++++++++++");
            } else {
                System.out.println("=================");
            }
            Thread.sleep(50);
        }
    }
}

class MyThread extends Thread {
    private boolean flag = false;

    public boolean isFlag(){
        return flag;
    }

    /**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see #start()
     * @see #stop()
     * @see #Thread(ThreadGroup, Runnable, String)
     */
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("flag = " + flag);
    }
}
